package com.xlf.service.system.mq;


import com.rabbitmq.client.Channel;
import com.xlf.pojo.dto.TestJudgeReq;
import com.xlf.pojo.system.SysOperLog;
import com.xlf.service.system.service.SysOperLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.SerializationUtils;

import java.io.IOException;
import static com.xlf.common.core.constant.MqConstant.Log.LOG_DEV_QUEUE;


@Service
@Slf4j
public class LogConsumer {


    @Autowired
    SysOperLogService sysOperLogService;


    @RabbitListener(queues = {LOG_DEV_QUEUE})
    private void LogDevConsumer(String msg, Message message, Channel channel) {

        try {

            byte[] body = message.getBody();
            SysOperLog sysOperLog = (SysOperLog) SerializationUtils.deserialize(body);
            sysOperLogService.insertOperlog(sysOperLog);
        } catch(Exception e) {
            e.printStackTrace();
        }finally {
            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
